﻿using Microsoft.Extensions.DependencyInjection;
using QPlcPlayer.Services;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace QPlcPlayer
{
	public partial class QTimerFrm : Form
	{
		public QTimerFrm()
		{
			InitializeComponent();
			InitTimerData();
		}

		private string QTimerStatusId { get; set; }


		public void InitTimerData()
		{
			var StatusKeyVal = new Dictionary<bool, int>{{false,0 },{true,1}};
			var info = AppRuntime.GetService().GetService<QTimerStatusService>().GetQTimerStatus(AppRuntime.CarNumber) ?? new Models.QTimerStatus();
			QTimerStatusId = info.ID;
			comboBox1.SelectedIndex = StatusKeyVal[info.IsTimer_PLC];
			comboBox2.SelectedIndex = StatusKeyVal[info.IsTimer_HuanKong];
			comboBox3.SelectedIndex = StatusKeyVal[info.IsTimer_JieMian];
			comboBox4.SelectedIndex = StatusKeyVal[info.IsLogger];
			numericUpDown1.Value = info.Interval_Timer_PLC;
			numericUpDown2.Value = info.Interval_Timer_HuanKong;
			numericUpDown3.Value = info.Interval_Timer_JieMian;
			numericUpDown4.Value = info.MinuteBeforeFeed;


		}

		private void button1_Click(object sender, EventArgs e)
		{
			var StatusKeyVal = new Dictionary<int, bool> { { 0, false}, { 1, true } };
			var info = new Models.QTimerStatus();
			info.ID = QTimerStatusId;
			info.CarNumber = AppRuntime.CarNumber;
			info.IsTimer_PLC = StatusKeyVal[comboBox1.SelectedIndex];
			info.IsTimer_HuanKong = StatusKeyVal[comboBox2.SelectedIndex];
			info.IsTimer_JieMian = StatusKeyVal[comboBox3.SelectedIndex];
			info.IsLogger=StatusKeyVal[comboBox4.SelectedIndex];

			info.Interval_Timer_PLC = numericUpDown1.Value;
			info.Interval_Timer_HuanKong = numericUpDown2.Value;
			info.Interval_Timer_JieMian = numericUpDown3.Value;
			info.MinuteBeforeFeed = numericUpDown4.Value;
			AppRuntime.GetService().GetService<QTimerStatusService>().AddOrUpdate(info);
			AppRuntime.QTimerStatus = info;
			MessageBox.Show("保存成功");

		}
	}
}
